<?php

namespace app\index\model;

use think\Db;
use think\facade\Cache;

class HtroleOperation
{
    const URLSS='access_urls';
    static function getUrl($roleId){

        @$key = URLSS.$roleId;
        @$urlss = Cache::get($key);
        if (empty($urlss)) {
            $url = Db::query('SELECT * FROM htoperation join htrole_operation on htoperation.id = htrole_operation.htoperation_id join htrole on htrole_operation.htrole_id = htrole.id WHERE htrole_operation.htrole_id=?', [$roleId]);
//            $urlh = [];
//        foreach ($url as $row){
//                $urlh[] = $row;
//            }
//        return $urlh;
            $list = array(array());
            foreach ($url as &$qx){
                $list[$qx['htoperation_id']] =array('name'=>$qx['role'],'title'=>$qx['operation'],'url'=>$qx['url']);
            }
            foreach ($url as &$qx){
                $list[$qx['p_id']]['children'][] =&$list[$qx['htoperation_id']];
            }
            $urlss = $list[0]['children'];

            Cache::set($key, $urlss);
         }
        return $urlss;
//        方式二
//        return Db::table('htoperation o')
//            ->join('htrole_operation r','o.id = r.htoperation_id')
//            ->where(['r.htrole_id'=>$roleId])
//            ->column('o.url');
    }

    
    function update($roleId,$operation){
//        $key = ACCSESS.$roleId;
//        Cache::rm($key);
    }
}
